﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Task5_NumberConverter
{
    class Task5_NumberConverter
    {
        static void Main(string[] args)
        {
            int number;
            GetNumberFromInput(out number);

            string numberToString = ConvertNumberToString(number);
            Console.WriteLine(numberToString);
        }

        private static string ConvertNumberToString(int number)
        {
            string result = "";
            int hundreds = number / 100;
            int remainder = number % 100;
            int tens = remainder / 10;
            int ones = remainder % 10;
            if (hundreds != 0)
            {                
                result += RecogniseNumber(hundreds) + " hundred ";                
                if (remainder == 0)
                {
                    return result;
                }
                if (tens == 0 && ones != 0)
                {
                    result += "and " + RecogniseNumber(ones);
                    return result;
                }
            }
            if (tens == 1)
            {
                if (hundreds != 0)
                {
                    result += "and ";
                }
                result += RecogniseNumberWithTensEqualToOne(remainder);
                return result;
            }
            else if (tens == 0)
            {
                result = RecogniseNumber(ones);
                return result;
            }
            else
            {
                result += RecogniseTens(tens);
                if (ones != 0)
                {
                    result += " " + RecogniseNumber(ones);
                }
                return result;
            }
        }

        private static string RecogniseTens(int number)
        {
            if (number == null)
            {
                throw new NullReferenceException();
            }
            if (number == 0)
            {
                throw new ArgumentException();
            }
            if (number == 1)
            {
                throw new ArgumentException();
            }

            string result = "";
            switch (number)
            {
                case 2:
                    result += "twenty";
                    break;
                case 3:
                    result += "thirty";
                    break;
                case 4:
                    result += "forty";
                    break;
                case 5:
                    result += "fifty";
                    break;
                case 6:
                    result += "sixty";
                    break;
                case 7:
                    result += "seventy";
                    break;
                case 8:
                    result += "eighty";
                    break;
                case 9:
                    result += "ninety";
                    break;
                default:
                    break;
            }
            return result;
        }

        private static string RecogniseNumberWithTensEqualToOne(int number)
        {
            if (number == null)
            {
                throw new NullReferenceException();
            }
            if (number / 10 != 1)
            {
                throw new ArgumentException();
            }
            int ones = number % 10;
            string result = "";
            switch (ones)
            {
                case 0:
                    result = "ten";
                    break;
                case 1:
                    result = "eleven";
                    break;
                case 2:
                    result = "twelve";
                    break;
                case 3:
                    result = "thirteen";
                    break;
                case 4:
                    result = "fourteen";
                    break;
                case 5:
                    result = "fifteen";
                    break;
                case 6:
                    result = "sixteen";
                    break;
                case 7:
                    result = "seventeen";
                    break;
                case 8:
                    result = "eighteen";
                    break;
                case 9:
                    result = "nineteen";
                    break;
                default:
                    break;
            }
            return result;
        }

        private static string RecogniseNumber(int number)
        {
            if (number == null)
            {
                throw new NullReferenceException();
            }
            string result = "";
            switch (number)
            {
                case 0:
                    break;
                case 1:
                    result = "one";
                    break;
                case 2:
                    result = "two";
                    break;
                case 3:
                    result = "three";
                    break;
                case 4:
                    result = "four";
                    break;
                case 5:
                    result = "five";
                    break;
                case 6:
                    result = "six";
                    break;
                case 7:
                    result = "seven";
                    break;
                case 8:
                    result = "eight";
                    break;
                case 9:
                    result = "nine";
                    break;
                default:
                    break;
            }
            return result;
        }

        private static void GetNumberFromInput(out int number)
        {
            Console.Write("Please, enter a number from the range [0 - 999]: ");
            string inputNumberLine = Console.ReadLine();
            bool parseStringResult = int.TryParse(inputNumberLine, out number);
            while (!parseStringResult || !IsValidNumberInput(number))
            {
                Console.Write("Incorrect input! Please, enter a number from the range [0 - 999]: ");
                inputNumberLine = Console.ReadLine();
                parseStringResult = int.TryParse(inputNumberLine, out number);
            }
        }

        private static bool IsValidNumberInput(int number)
        {
            if (number == null)
            {
                throw new NullReferenceException();
            }
            bool result = 0 <= number && number <= 999;
            return result;
        }
    }
}
